package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.common.dto.send.Receiver;
import com.aliyun.messagemgt.converter.MsgHandleLogConverter;
import com.aliyun.messagemgt.domain.repository.MsgHandleLogRepository;
import com.aliyun.messagemgt.dto.MsgHandleLog;
import com.aliyun.messagemgt.dto.query.MsgHandleLogQuery;
import com.aliyun.messagemgt.dto.vo.MsgHandleLogResultVO;
import com.aliyun.messagemgt.dto.vo.SmsTrendResultVO;
import com.aliyun.messagemgt.entities.MsgHandleLogEntity;
import com.aliyun.messagemgt.repository.mappers.MsgHandleLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author hzw
 * @Date 2020/12/15 15:21
 * @Version 1.0
 */
@Service
public class MsgHandleLogMybatisRepository implements MsgHandleLogRepository {
    @Autowired
    private MsgHandleLogMapper msgHandleLogMapper;


    /**
     * 查询推送失败数量
     *
     * @param msgId
     * @return
     */
    @Override
    public int queryPushResult(Long msgId) {
        return msgHandleLogMapper.queryPushResult(msgId);
    }

    /**
     * 查询消息处理记录表3个月前数据
     *
     * @param historyDay
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<MsgHandleLog> queryHistoryMsg(LocalDate historyDay, int pageNum, int pageSize) {
        List<MsgHandleLogEntity> msgHandleLogEntityList = msgHandleLogMapper.queryHistoryMsg(historyDay, pageNum, pageSize);
        List<MsgHandleLog> msgHandleLogList = MsgHandleLogConverter.INSTANCE.msgHandleLogEntityList2msgHandleLogList(msgHandleLogEntityList);
        return msgHandleLogList;
    }

    /**
     * 查询消息处理记录表3个月前数据
     *
     * @param historyDay
     * @param sourceName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<MsgHandleLog> queryDevHistoryMsg(LocalDate historyDay, String sourceName, int pageNum, int pageSize) {
        List<MsgHandleLogEntity> msgHandleLogEntityList = msgHandleLogMapper.queryDevHistoryMsg(historyDay, sourceName, pageNum, pageSize);
        List<MsgHandleLog> msgHandleLogList = MsgHandleLogConverter.INSTANCE.msgHandleLogEntityList2msgHandleLogList(msgHandleLogEntityList);
        return msgHandleLogList;
    }

    /**
     * 删除消息处理记录表中数据
     *
     * @param msgHandleLogs
     * @return
     */
    @Override
    public int deleteHistoryMsg(List<MsgHandleLog> msgHandleLogs) {
        return msgHandleLogMapper.deleteHistoryMsg(msgHandleLogs);
    }

    /**
     * 更新用户是否已读消息
     *
     * @param msgHandleLogQuery
     * @return
     */
    @Override
    public int updateStateRead(MsgHandleLogQuery msgHandleLogQuery) {
        return msgHandleLogMapper.updateStateRead(msgHandleLogQuery);
    }

    /**
     * 查询短信概览统计（以天为维度）
     *
     * @param msgSourceId 消息源
     * @param beginDate   开始时间 yyyy-MM-dd
     * @param endDate     结束时间 yyyy-MM-dd
     * @param tempEndDate 临时表结束时间 yyyy-MM-dd
     * @return map
     */
    @Override
    public List<SmsTrendResultVO.SmsTrend> queryDayTrend(String msgSourceId, String beginDate, String endDate, String tempEndDate) {
        return msgHandleLogMapper.queryDayTrend(msgSourceId, beginDate, endDate, tempEndDate);
    }

    /**
     * 查询短信概览统计（以月度为维度）
     *
     * @param msgSourceId 消息源
     * @param beginDate   开始时间 yyyy-MM
     * @param endDate     结束时间 yyyy-MM
     * @param monthLimit  limit 值
     * @return map
     */
    @Override
    public List<SmsTrendResultVO.SmsTrend> queryMonthTrend(String msgSourceId, String beginDate, String endDate, Integer monthLimit) {
        return msgHandleLogMapper.queryMonthTrend(msgSourceId, beginDate, endDate, monthLimit);
    }

    @Override
    public int batchMsgHandleLogInsert(List<MsgRecordVO> msgRecordVos) {
        return msgHandleLogMapper.batchMsgHandleLogInsert(msgRecordVos);
    }

    @Override
    public List<MsgHandleLogResultVO> queryErrorMsgHandleLogsBySourceId(String msgSourceId, Date beginTime, Date endTime) {
        return msgHandleLogMapper.queryErrorMsgHandleLogsBySourceId(msgSourceId, beginTime, endTime);
    }

    @Override
    public List<MsgHandleLogResultVO> queryErrorMsgHandleLogsByChannelName(Integer channelId, Date beginTime, Date endTime) {
        return msgHandleLogMapper.queryErrorMsgHandleLogsByChannelName(channelId, beginTime, endTime);
    }

    @Override
    public List<MsgHandleLogResultVO> queryErrorMsgHandleLogsByChannelType(Integer channelType, Date beginTime, Date endTime) {
        return msgHandleLogMapper.queryErrorMsgHandleLogsByChannelType(channelType, beginTime, endTime);
    }

    @Override
    public void updateByMsgId(MsgHandleLog msgHandleLog) {
        int count = msgHandleLogMapper.updateByMsgId(msgHandleLog);
    }

    @Override
    public void updatePushResult(MsgHandleLog msgHandleLog, List<Receiver> receiverList) {
        msgHandleLogMapper.updatePushResult(msgHandleLog, receiverList);
    }
}
